new Vue({
    el: '#app',
    data: function () {
        const validateUserName = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('请输入用户名'));
            } else {
                callback();
            }
        };
        const validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                callback();
            }
        };
        const validateCode = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入验证码'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                signature: '',
                id: '',
                pid: '',
                date: '',
                cookie: '',
            },
            rules: {
                // loginName: [
                //     {validator: validateUserName, trigger: 'blur'}
                // ],
                // password: [
                //     {validator: validatePass, trigger: 'blur'}
                // ],
                // code: [
                //     {validator: validateCode, trigger: 'blur'}
                // ],
            },
            captchaUrl: '/captcha'
        };
    },
    methods: {
        getConfig() {
            $get("/yimiao/getConfig", null).then((data)=>{
                this.ruleForm = data;
            })
        },
        // 提交登录表单
        updateConfig(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // 表单校验后, 提交登录
                    $post("/yimiao/updateHeader", {
                        id: this.ruleForm.id,
                        pid: this.ruleForm.pid,
                        cookie: this.ruleForm.cookie,
                        date: this.ruleForm.date
                    }).then((e)=>{
                        alert(e)
                    }).catch((err) => {
                        console.log('err---', err)
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 刷新验证码
        refreshCode() {
            this.captchaUrl = "/captcha?s=" + Math.random()
        },
        // 监听回车键登录
        enterKeyup(event) {
            if (13 === event.keyCode) {
                this.login('ruleForm');
            }
        },
    },
    created() {
        this.getConfig();
    },
    mounted() {
        document.addEventListener("keyup",  this.enterKeyup)
    },
    destroy() {
        document.removeEventListener("keyup", this.enterKeyup);
    }
})